package com.seabook.happy.website.servlets;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.datastore.Text;
import com.seabook.happy.website.daos.BlogArticleDao;
import com.seabook.happy.website.pojos.BlogArticle;


public class BlogUpdateServlet extends HttpServlet {
	private static final long serialVersionUID = 5141658490949468457L;
	
	private BlogArticleDao atricleDao = new BlogArticleDao();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String action = req.getParameter("action");
		if ("new".equals(action)) {
			postArticle(req, resp);
		} else if ("del".equals(action)) {
			
		} else if ("update".equals(action)) {
			
		} 
	}
	
	private void postArticle(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String title = req.getParameter("title");
		Text content = new Text(req.getParameter("article-content"));
		BlogArticle article = new BlogArticle();
		article.setTitle(title);
		article.setContent(content);
		article.setCreatedDate(new Date());
		article.setModifiedDate(new Date());
		atricleDao.saveArticle(article);
		resp.sendRedirect("blog.jsp");
	}
	
}
